<?php
namespace app\wechat\controller;

use app\dsc\controller\BaseController;

class TestController extends BaseController
{
    public function send()
    {
        $url="http://www.airbaba.cn/mobile/index.php?m=wechat&c=api&a=syncConfig";
        $result=$this->post($url,["url"=>"http://www.airbaba.cn/mobile/index.php"]);
        $config=json_decode($result,true);
        $access_token=$config["access_token"];

        $message=[
            "touser"=>"ojh1C5ucUzzQgqLa9wJUytP-uBEY",
            "template_id"=>"pmHYFI-J6gDVkmmLCz4KUC73QOP32OBW8jv7RWdR6uQ",//"d_CcrFkpefu3IQwEek28jUeJTav8q2mH6d1masyxCZg",
            "url"=>"http://serve.airbaba.cn/maintain_man/public/award",
            "topcolor"=>"#FF0000",
            "data"=>[
                "first"=>[
                    "value"=>"维修师傅认证通过",
                    "color"=>"#173177"
                ],
                "remark"=>[
                    "value"=>"请点击领取奖励",
                    "color"=>"#173177"
                ],
                "keyword1"=>[
                    "value"=>date("Y年m月d日 H:i:s"),
                    "color"=>"#173177"
                ],
                "keyword2"=>[
                    "value"=>"审核通过",
                    "color"=>"#173177"
                ],
            ],
        ];
        $result=$this->post("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$access_token",$message);
        
        return json_encode($result,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);

    }
    protected function post($url,$data)
    {
        $data=json_encode($data,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);// POST方式发送
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);// post的数据
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
    /**
     * 显示大商创微信自动登录后携带来的参数
     */
    public function debug()
    {
        $data=input();
        return json_encode($data,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
    }
}
